//
//  AddressManagementTableViewCell.m
//  BuyWhat
//
//  Created by ios on 2016/10/31.
//  Copyright © 2016年 吕金状. All rights reserved.
//

#import "AddressManagementTableViewCell.h"

@interface AddressManagementTableViewCell ()

@property (nonatomic , strong) UILabel *nameLabel;
@property (nonatomic , strong) UILabel *phoneNumberLabel;
@property (nonatomic , strong) UILabel *addressLabel;
@property (nonatomic , strong) UILabel *defaultLabel;
@property (nonatomic , strong) UIButton *choosebutton;
@property (nonatomic , strong) UIImageView *editorImageView;
@property (nonatomic , strong) UIButton *editorButton;
@property (nonatomic , strong) UIImageView *deleteImageView;
@property (nonatomic , strong) UIButton *deleteButton;
@property (nonatomic , strong) UIView *lineView;

@end

@implementation AddressManagementTableViewCell

- (id)initWithCellIdentifier:(NSString *)cellID{
    if (self = [super initWithCellIdentifier:cellID]) {
        
        self.selectionStyle = UITableViewCellStyleDefault;
        
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = DEF_FontSize_15;
        [self.contentView addSubview:_nameLabel];
        
        _phoneNumberLabel = [[UILabel alloc]init];
        _phoneNumberLabel.font = DEF_FontSize_15;
        [self.contentView addSubview:_phoneNumberLabel];
        
        _addressLabel = [[UILabel alloc]init];
        _addressLabel.textColor = getColor(textColor);
        _addressLabel.font = DEF_FontSize_15;
        _addressLabel.numberOfLines = 0;
        [self.contentView addSubview:_addressLabel];
        
        _choosebutton = [[UIButton alloc]init];
        [_choosebutton addTarget:self action:@selector(choosebuttonClick) forControlEvents:UIControlEventTouchUpInside];
        _choosebutton.layer.masksToBounds = YES;
        _choosebutton.layer.cornerRadius = 23 / WIDTH_5S_SCALE / 2;
        [self.contentView addSubview:_choosebutton];
        
        _defaultLabel = [[UILabel alloc]init];
        _defaultLabel.text = @"默认";
        _defaultLabel.textColor = getColor(textColor);
        _defaultLabel.font = DEF_FontSize_15;
        _defaultLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(choosebuttonClick)];
        [_defaultLabel addGestureRecognizer:tap1];
        [self.contentView addSubview:_defaultLabel];
        
        _editorImageView = [[UIImageView alloc]init];
        _editorImageView.image = [UIImage imageNamed:@"button_edit"];
        _editorImageView.contentMode = UIViewContentModeScaleAspectFit;
        _editorImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(editorButtonClick)];
        [_editorImageView addGestureRecognizer:tap];
        [self.contentView addSubview:_editorImageView];
        
        _editorButton = [[UIButton alloc]init];
        [_editorButton setTitle:@"编辑" forState:UIControlStateNormal];
        [_editorButton setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        _editorButton.titleLabel.font = DEF_FontSize_15;
        [_editorButton addTarget:self action:@selector(editorButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_editorButton];
        
//        _deleteImageView = [[UIImageView alloc]init];
//        _deleteImageView.image = [UIImage imageNamed:@"button_Cancelattention"];
//        [self.contentView addSubview:_deleteImageView];
//        
//        _deleteButton = [[UIButton alloc]init];
//        [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
//        _deleteButton.titleLabel.font = DEF_FontSize_15;
//        [_deleteButton setTitleColor:getColor(textColor) forState:UIControlStateNormal];
//        [_deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
//        [self.contentView addSubview:_deleteButton];
        
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = getColor(dividerColor);
        [self.contentView addSubview:_lineView];
        [self layoutFrame];
    }
    return self;
}

- (void)layoutFrame{
    __weak __typeof(self)weakSelf = self;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).offset(23 / WIDTH_5S_SCALE);
        make.left.equalTo(weakSelf).offset(10 / WIDTH_5S_SCALE);
        make.height.mas_offset(15 / WIDTH_5S_SCALE);
    }];
    
    [self.phoneNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).offset(23 / WIDTH_5S_SCALE);
        make.right.equalTo(weakSelf).offset(- 90 / WIDTH_5S_SCALE);
        make.height.mas_offset(15 / WIDTH_5S_SCALE);
    }];
    
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.nameLabel.mas_bottom).offset(15 / WIDTH_5S_SCALE);
        make.left.equalTo(weakSelf).offset(10 / WIDTH_5S_SCALE);
        make.width.mas_offset(SCREEN_WIDTH - 20 / WIDTH_5S_SCALE);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weakSelf.mas_bottom).offset(- 40);
        make.left.equalTo(weakSelf).offset(15 / WIDTH_5S_SCALE);
        make.width.mas_offset(SCREEN_WIDTH - 15 / WIDTH_5S_SCALE);
        make.height.mas_offset(0.5);
    }];
    
    [self.choosebutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.lineView.mas_bottom).offset(0);
        make.left.equalTo(weakSelf).offset(10 / WIDTH_5S_SCALE);
        make.width.mas_offset(23 / WIDTH_5S_SCALE);
        make.height.mas_offset(40);
    }];
    
    [self.defaultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.lineView.mas_bottom).offset(0);
        make.left.equalTo(weakSelf.choosebutton.mas_right).offset(10 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(40);
    }];
    
//    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(weakSelf.lineView.mas_bottom).offset(0);
//        make.right.equalTo(weakSelf).offset(- 10 / WIDTH_5S_SCALE);
//        make.width.mas_offset(50 / WIDTH_5S_SCALE);
//        make.height.mas_offset(45 / WIDTH_5S_SCALE);
//    }];
//    
//    [self.deleteImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(weakSelf.lineView.mas_bottom).offset(11 / WIDTH_5S_SCALE);
//        make.right.equalTo(weakSelf.deleteButton.mas_left).offset(- 10 / WIDTH_5S_SCALE);
//        make.width.mas_offset(20 / WIDTH_5S_SCALE);
//        make.height.mas_offset(23 / WIDTH_5S_SCALE);
//    }];
    
    [self.editorButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.lineView.mas_bottom).offset(0);
        make.right.mas_equalTo(- 20 / WIDTH_5S_SCALE);
        make.width.mas_offset(50 / WIDTH_5S_SCALE);
        make.height.mas_offset(40);
    }];
    
    [self.editorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.lineView.mas_bottom).offset(0);
        make.right.equalTo(weakSelf.editorButton.mas_left).offset(0 / WIDTH_5S_SCALE);
        make.width.mas_offset(20 / WIDTH_5S_SCALE);
        make.bottom.mas_equalTo(0);
    }];
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.nameLabel.text = model[@"ua_name"];
    self.phoneNumberLabel.text = model[@"ua_mobile"];
    self.addressLabel.text = model[@"ua_address"];
    if ([model[@"ua_default"] isEqualToString:@"1"]) {
        [_choosebutton setImage:[UIImage imageNamed:@"button_Selected"] forState:UIControlStateNormal];
    }else{
        [self.choosebutton setImage:[UIImage imageNamed:@"button_Unchecked"] forState:UIControlStateNormal];
    }
    
}

#pragma mark - click

/**
 选择地址点击事件
 */
- (void)choosebuttonClick{
    if ([self.delegade respondsToSelector:@selector(getDefault:)]) {
        [self.delegade getDefault:self];
    }
}

/**
 编辑点击事件
 */
- (void)editorButtonClick{
    NSLog(@"点击了编辑");
    if ([self.delegade respondsToSelector:@selector(getEditorClcik:)]) {
        [self.delegade getEditorClcik:self];
    }
}

/**
 删除点击事件
 */
- (void)deleteButtonClick{
     NSLog(@"点击了删除");
    if ([self.delegade respondsToSelector:@selector(getDeleteClick:)]) {
        [self.delegade getDeleteClick:self];
    }
}

@end
